{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Google Colab Tutorial",
      "provenance": [],
      "collapsed_sections": [],
      "toc_visible": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ca2CpPPUvO-h"
      },
      "source": [
        "# **Google Colab Tutorial**\n",
        "\n",
        "\n",
        "Should you have any question, contact TA via <br/> ntu-ml-2021spring-ta@googlegroups.com\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "xIN7RF4wjgHk"
      },
      "source": [
        "<p><img alt=\"Colaboratory logo\" height=\"45px\" src=\"/img/colab_favicon.ico\" align=\"left\" hspace=\"10px\" vspace=\"0px\"></p>\r\n",
        "\r\n",
        "<h1>What is Colaboratory?</h1>\r\n",
        "\r\n",
        "Colaboratory, or \"Colab\" for short, allows you to write and execute Python in your browser, with \r\n",
        "- Zero configuration required\r\n",
        "- Free access to GPUs\r\n",
        "- Easy sharing\r\n",
        "\r\n",
        "Whether you're a **student**, a **data scientist** or an **AI researcher**, Colab can make your work easier. Watch [Introduction to Colab](https://www.youtube.com/watch?v=inN8seMm7UI) to learn more, or just get started below!\r\n",
        "\r\n",
        "You can type python code in the code block, or use a leading exclamation mark ! to change the code block to bash environment to execute linux code."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "IrAxlhp3VBMD"
      },
      "source": [
        "To utilize the free GPU provided by google, click on \"Runtime\"(執行階段) -> \"Change Runtime Type\"(變更執行階段類型). There are three options under \"Hardward Accelerator\"(硬體加速器), select \"GPU\". \r\n",
        "* Doing this will restart the session, so make sure you change to the desired runtime before executing any code.\r\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "CLUWxZKbvQpx"
      },
      "source": [
        "import torch\n",
        "torch.cuda.is_available() # is GPU available\n",
        "# Outputs True if running with GPU"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "EAM_tPQAELh0"
      },
      "source": [
        "**1. Download Files via google drive**\n",
        "\n",
        "  A file stored in Google Drive has the following sharing link：\n",
        "\n",
        "  https://drive.google.com/open?id=1duQU7xqXRsOSPYeOR0zLiSA8g_LCFzoV\n",
        "  \n",
        "  The random string after \"open?id=\" is the **file_id** <br />\n",
        "![](https://i.imgur.com/33SW1WZ.png)\n",
        "\n",
        "  It is possible to download the file via Colab knowing the **file_id**, using the following command.\n",
        "\n",
        "\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "XztYEj0oD7J3"
      },
      "source": [
        "# Download the file with file_id \"1duQU7xqXRsOSPYeOR0zLiSA8g_LCFzoV\", and rename it to Minori.jpg\n",
        "!gdown --id '1duQU7xqXRsOSPYeOR0zLiSA8g_LCFzoV' --output Minori.jpg"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Gg3T23LXG-eL"
      },
      "source": [
        "# List all the files under the working directory\n",
        "!ls"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "38dcGQujOVWM"
      },
      "source": [
        "Exclamation mark (!) starts a new shell, does the operations, and then kills that shell, while percentage (%) affects the process associated with the notebook"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "dOQxjfAZAsys"
      },
      "source": [
        "It can be seen that `Minori.jpg` is saved the the current working directory. \r\n",
        "\r\n",
        "The working space is temporary, once you close the browser, the file will be gone.\r\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "wLUPcHuNHF8u"
      },
      "source": [
        "Clicking on the folder icon will give you the visuallization of the file structure\n",
        "<br/>\n",
        "&emsp;&emsp;![image.png]()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "MXp98PijHkrk"
      },
      "source": [
        "There should be a file named `Minori.jpg`, if you do not see it, click the icon in the middle (refresh button) <br/>\n",
        "&emsp;&emsp;![](https://i.imgur.com/CNBTH23.png)\n",
        "<br />\n",
        "You can double click on the file to view the image.\n",
        "\n",
        "\n",
        "&emsp;&emsp; \n",
        "![](https://i.imgur.com/h2PLMrq.png)\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "k_gmTo9NKtu9"
      },
      "source": [
        "**2. Mounting Google Drive**\n",
        "\n",
        "  One advantage of using google colab is that connection with other google services such as Google Drive is simple. By mounting google drive, the working files can be stored permanantly. After executing the following code block, log in to the google account and copy the authentication code to the input box to finish the process."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ImETTQKkL2l4"
      },
      "source": [
        "from google.colab import drive # Import a library named google.colab\n",
        "drive.mount('/content/drive', force_remount=True) # mount the content to the directory `/content/drive`"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "BmvzTF5IJ6TL"
      },
      "source": [
        "from google.colab import drive\n",
        "drive.mount('/content/drive')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "AkmayCmGMD03"
      },
      "source": [
        "After mounting the drive, the content of the google drive will be under a directory named `MyDrive`, check the file structure for such a folder to confirm the execution of the code."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "kDrO_DjBMW5D"
      },
      "source": [
        "There is also an icon for mounting google drive. The icon will automatically generate the code above.\n",
        "\n",
        "![](https://i.imgur.com/hM9Jgi7.png)  \n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "UhKhwipoMvXF"
      },
      "source": [
        "After mounting the drive, all the chnages will be synced with the google drive.\n",
        "Since models could be quite large, make sure that your google drive has enough space. You can apply for a gsuite drive which has unlimited space using your studentID (until 2022/07). \n",
        "https://www.cc.ntu.edu.tw/chinese/services/serv_i06.asp\n",
        "http://www.cc.ntu.edu.tw/english/spotlight/2016/a105038.asp"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "UT0TEPRS7KF6"
      },
      "source": [
        "%cd /content/drive/MyDrive \r\n",
        "#change directory to google drive\r\n",
        "!mkdir ML2021 #make a directory named ML2021\r\n",
        "%cd ./ML2021 \r\n",
        "#change directory to ML2021"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Oj13Q58QerAx"
      },
      "source": [
        "Use bash command pwd to output the current directory"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-S8l1-ReepkS"
      },
      "source": [
        "!pwd #output the current directory"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "qSSvrDaBiDrP"
      },
      "source": [
        "Repeat the downloading process, this time, the file will be stored permanently in your google drive."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "b39YMYicASvP"
      },
      "source": [
        "# Download the file with file_id \"1duQU7xqXRsOSPYeOR0zLiSA8g_LCFzoV\", and rename it to Minori.jpg\r\n",
        "!gdown --id '1duQU7xqXRsOSPYeOR0zLiSA8g_LCFzoV' --output Minori.jpg"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "D0URgikZXl5I"
      },
      "source": [
        "TA will provide the homework data using code similar to the code above. The data could also be stored in the google drive and loaded from there."
      ]
    }
  ]
}